package com.jgsu.farm.service.impl;

import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.*;
import com.jgsu.farm.service.DiagnosisService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiagnosisServiceImpl implements DiagnosisService {
    @Autowired
    InquiryMapper inquiryMapper;
    @Autowired
    RuleMapper ruleMapper;
    @Autowired
    ModelMapper modelMapper;
    @Autowired
    StressCategoryMapper stressCategoryMapper;
    @Autowired
    DiagnosisMapper diagnosisMapper;
    @Override
    public void add(Diagnosis diagnosis) {
        Inquiry inquiry = inquiryMapper.selectByPrimaryKey(diagnosis.getInquiryid());
        Rule rule = ruleMapper.selectByPrimaryKey(diagnosis.getRuleid());
        Model model = modelMapper.selectByPrimaryKey(diagnosis.getModelid());
        StressCategory stressCategory = stressCategoryMapper.selectByPrimaryKey(diagnosis.getType());
        if (inquiry != null && rule != null && model != null && stressCategory != null){
            diagnosisMapper.insert(diagnosis);
        }else {
            throw new DataAddException("604", "Diagnosis表数据添加异常");
        }
    }

    @Override
    public void delete(Integer diagnosisid) {
        int i = diagnosisMapper.deleteByPrimaryKey(diagnosisid);
        if (i == 0){
            throw new  DataNotExistException("603", "不存在该id,Diagnosis表删除失败");
        }
    }

    @Override
    public PageInfo<Diagnosis> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Diagnosis> diagnosis = diagnosisMapper.selectAll();
        PageInfo<Diagnosis> pageInfoDiagnosisList = new PageInfo<Diagnosis>(diagnosis);
        return pageInfoDiagnosisList;
    }
}
